package com.itheima.health.service.impl;

import com.alibaba.dubbo.common.utils.StringUtils;
import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.health.dao.UserDao;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.exception.MyException;
import com.itheima.health.pojo.Role;
import com.itheima.health.pojo.User;
import com.itheima.health.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *
 * </p>
 *
 * @author: Eric
 * @since: 2021/1/13
 */
@Service(interfaceClass = UserService.class)
public class UserServiceImpl implements UserService {


    @Autowired
    private UserDao userDao;

    /**
     * 通过用户名查询用户角色权限
     *
     * @param username
     * @return
     */
    @Override
    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }


    @Override
    public List<User> findAll() {


        return userDao.findAll();
    }

    /**
     * 添加检查项
     *
     * @param user
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void add(User user, Integer[] roles) {
        String username = user.getUsername();
        if (StringUtils.isEmpty(username)) {
            throw new MyException("角色名称不能为空");
        }
        if (username.length() > 32) {
            throw new MyException("角色的长度不能超过32位");
        }
        //设置初始密码
        BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
        user.setPassword(encoder.encode("123456"));
        userDao.add(user);
        if (roles != null && roles.length > 0) {
            Integer userId = user.getId();
            for (Integer roleId : roles) {
                userDao.addUserRole(userId, roleId);
            }
        }
    }

    /**
     * 检查项的分页查询
     *
     * @param queryPageBean
     * @return
     */
    @Override
    public PageResult<User> findPage(QueryPageBean queryPageBean) {
        // pageSize能无限大吗？参数是从前端来的，100W，后台限制大小
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 条件查询
        if (StringUtils.isNotEmpty(queryPageBean.getQueryString())) {
            // 有查询条件， 模糊查询
            queryPageBean.setQueryString("%" + queryPageBean.getQueryString() + "%");
        }
        // page extends arrayList
        Page<User> page = userDao.findByCondition(queryPageBean.getQueryString());
        PageResult<User> pageResult = new PageResult<User>(page.getTotal(), page.getResult());
        return pageResult;
    }

    /**
     * 通过id查询
     *
     * @param id
     * @return
     */
    @Override
    public User findById(int id) {
        return userDao.findById(id);
    }

    /**
     * 修改检查项
     *
     * @param user
     */
    @Override
    @Transactional
    public void update(User user, Integer[] roles) {
        String username = user.getUsername();
        if (StringUtils.isEmpty(username)) {
            throw new MyException("角色名称不能为空");
        }
        if (username.length() > 32) {
            throw new MyException("角色的长度不能超过32位");
        }
        Integer userId = user.getId();
        //删除角色与菜单的关系
        userDao.deleteRoleUser(userId);
        userDao.update(user);
        if (roles != null && roles.length > 0) {

            for (Integer roleId : roles) {
                userDao.addUserRole(userId, roleId);
            }
        }


    }

    /**
     * 通过id删除
     *
     * @param id
     * @throws MyException
     */
    @Override
    @Transactional
    public void deleteById(int id) {
        // 删除
        userDao.deleteRoleUser(id);
        userDao.deleteById(id);
    }

    @Override
    public void resetPassword(@Param("id")Integer id,@Param("encodeId")String encodeId) throws MyException {
        userDao.resetPassword(id,encodeId);
    }

    /**
     * 查询角色信息
     *
     * @return
     */
    @Override
    public List<Role> findByMenu() {
        return userDao.findByMenu();
    }

    /**
     * 查询角色id
     *
     * @param id
     * @return
     */
    @Override
    public List<Integer> findByMenuRole(int id) {
        return userDao.findByMenuRole(id);
    }
}
